package com.xxx.springboot.task;

import java.util.concurrent.TimeUnit;

import com.xxx.springboot.service.impl.SpringUtil;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import lombok.extern.log4j.Log4j2;

/**
 * demo task
 **/
@Log4j2
@Component
@EnableScheduling
public class DemoTask {

//    @Scheduled(fixedRate = 1000 * 10)
//    @Scheduled(cron = "0 0,29,59 0 * * ?")
    public void demo () {
    	log.info("DemoTask start......");
    	StringRedisTemplate stringRedisTemplate = SpringUtil.getBean(StringRedisTemplate.class);
    	
        //分布式锁，抢到锁的节点执行
        if (stringRedisTemplate.opsForValue().setIfAbsent("lock:demoTask", "ok", 15, TimeUnit.SECONDS)) {
        	log.info("demoTask is begin to excute");
//            doWork();
        } else {
        	log.error("demoTask is excuting....");
        }
        log.info("DemoTask end.");
    }

}
